package boot

import (
	"github.com/go-kratos/kratos/contrib/log/zap/v2"
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/tracing"
	uberZap "go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"kratos_cron_task/internal/conf"
	"os"
)

type BootLog struct {
	conf *conf.Bootstrap
}

func (b *BootLog) Run() log.Logger {
	zapLogger := b.Setting()
	logger := log.With(zap.NewLogger(zapLogger),
		"ts", log.Timestamp("2006-01-02 15:04:05.000"),
		"caller", log.DefaultCaller,
		"service.id", b.conf.Server.Id,
		"service.name", b.conf.Server.Name,
		"service.version", b.conf.Server.Version,
		"trace.id", tracing.TraceID(),
		"span.id", tracing.SpanID())
	return logger
}

func (b *BootLog) Setting(opts ...uberZap.Option) *uberZap.Logger {
	var core zapcore.Core
	core = zapcore.NewCore(
		zapcore.NewJSONEncoder(zapcore.EncoderConfig{
			LevelKey:    "level",
			EncodeLevel: zapcore.CapitalLevelEncoder,
		}), // 编码器配置
		zapcore.NewMultiWriteSyncer(
			zapcore.AddSync(os.Stdout)),
		uberZap.NewAtomicLevelAt(zapcore.InfoLevel), // 日志级别
	)
	return uberZap.New(core, opts...)
}
